
/*******************************/
/** ENTREPRENEUR - BANKRUPTCY **/
/*******************************/



double focB(const double xval, void * params) // JUST A FUNCTION, DOES NOT CONSTITUTE A VALUE FUNCTION
{
    struct paramsgoldenRB *focparams= (struct paramsgoldenRB *) params;
    
    double *expE_WWE = focparams->expE_WWE_ST;
    double *expE_UnE = focparams->expE_UnE_ST;
#if OPT_compute_C_flow == 1
    double *C_expE_WWE = focparams->C_expE_WWE_ST;
    double *C_expE_UnE = focparams->C_expE_UnE_ST;
#endif
#if SEA == 1
    double *expE_UiE = focparams->expE_UiE_ST;
#if OPT_compute_C_flow == 1
    double *C_expE_UiE = focparams->C_expE_UiE_ST;
#endif
#endif
    
    int igridCOHfoc=focparams->igridCOHST; int tgridfoc=focparams->tgridST; int zgridfoc=focparams->zgridST;
    int Jinsured=focparams->JinsuredST;
    
    double cons, xgrid, dxgrid, valfoc;
    double tempUnE, tempWWE, tempUiE, C_tempUnE, C_tempWWE, C_tempUiE;
    double searcheffWW, diffValue, diffgrid, ddiffgrid;
    int iverif, ixgrid, inxE,inxE1, idiffgrid;
    
    
    // CONSUMPTION //
    cons = findconsRB(xval, gridCOH[igridCOHfoc]);
    
    // ON THE GRID REDEFINITION //
    xgrid = invexpspace(xval,Gridmin,Gridmax,Echelle1,maxgrid);
    dxgrid = weightinter(xval, xgrid, &ixgrid, grid, maxgrid);
    if ((xval>(Gridmax))){printf("focB: (xval>(Gridmax)) %20.15f\n",xval);getchar();}
    if ((ixgrid>=(maxgrid-1))){printf("focB: ixgrid>=(maxgrid-1) %d\n",ixgrid);getchar();}
    if ((cons<=0.0)){printf("focB: consoFOC<0.0 %20.15f\t%20.15f\t%20.15f\n",xval,cons, findconsRB(0.0, gridCOH[igridCOHfoc]));getchar();}
    if ((xval<(0.0))){printf("focB_1: (xval<(0.0)) %20.15f\n",xval);getchar();}
    
    
    // CONTINUATION VALUE //
    inxE  = inxE(ixgrid,tgridfoc,zgridfoc);
    inxE1 = inxE((ixgrid+1),tgridfoc,zgridfoc);
    
    tempWWE       = inter1d(dxgrid,expE_WWE[inxE],expE_WWE[inxE1]);
    tempUnE       = inter1d(dxgrid,expE_UnE[inxE],expE_UnE[inxE1]);
#if OPT_compute_C_flow == 1
    C_tempWWE     = inter1d(dxgrid,C_expE_WWE[inxE],C_expE_WWE[inxE1]);
    C_tempUnE     = inter1d(dxgrid,C_expE_UnE[inxE],C_expE_UnE[inxE1]);
#endif
#if SEA == 1
    if(Jinsured == 1){
        tempUiE     = inter1d(dxgrid,expE_UiE[inxE],expE_UiE[inxE1]);
#if OPT_compute_C_flow == 1
        C_tempUiE   = inter1d(dxgrid,C_expE_UiE[inxE],C_expE_UiE[inxE1]);
#endif
    }
#endif
    
    
    // FIND THE OPTIMAL EFFORT LEVEL
    if(Jinsured == 0){diffValue = max(0.000000000001,(betapar*(tempWWE - tempUnE)));}
#if SEA == 1
    if(Jinsured == 1){diffValue = max(0.000000000001,(betapar*(tempWWE - tempUiE)));}
#endif
    diffgrid    = invexpspace(diffValue,gridminsearch,gridmaxsearch,EchelleW,maxsearch);
    ddiffgrid   = weightinter(diffValue, diffgrid, &idiffgrid, diffvalues, maxsearch);
    searcheffWW = inter1d(ddiffgrid,search1D_W[inxS(idiffgrid, tgridfoc)],search1D_W[inxS(idiffgrid+1, tgridfoc)]);
    focparams->searchWoutST=searcheffWW;
    
    
    // VALUE FUNCTION
    if(Jinsured == 0){
        valfoc           = utilc(cons,1) + disutilityW(searcheffWW) + betapar*(piW(searcheffWW)*tempWWE + (1.0-piW(searcheffWW))*tempUnE);
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*(piW(searcheffWW)*C_tempWWE + (1.0-piW(searcheffWW))*C_tempUnE);
#endif
    }
#if SEA == 1
    if(Jinsured == 1){
        valfoc           = utilc(cons,1) + disutilityW(searcheffWW) + betapar*(piW(searcheffWW)*tempWWE + (1.0-piW(searcheffWW))*tempUiE);
#if OPT_compute_C_flow == 1
        focparams->C_val = utilc(cons,1) + betapar*(piW(searcheffWW)*C_tempWWE + (1.0-piW(searcheffWW))*C_tempUiE);
#endif
    }
#endif
    
    
    valfoc=-valfoc;
    
    return valfoc;
}




